/*发布页面示例：取数据库数据 渲染到页面 */

/*往dom追加html :ie8不支持HTMLElement*/
HTMLElement.prototype.appendHTML = function(html) {
    var divTemp = document.createElement("div"), nodes = null
        // 文档片段，一次性append，提高性能
        , fragment = document.createDocumentFragment();
    divTemp.innerHTML = html;
    nodes = divTemp.childNodes;
    for (var i=0, length=nodes.length; i<length; i+=1) {
        fragment.appendChild(nodes[i].cloneNode(true));
    }
    this.appendChild(fragment);
    // 据说下面这样子世界会更清净
    nodes = null;
    fragment = null;
}
function renderData(data) {
    var html = data.html;
    var javascript = data.js;
    var css = data.css;
    var canvasStyle = JSON.parse(data.canvasStyle);


    console.log(dependency.uiFrame)
    if(dependency.uiFrame === 'vue' ){
        //参考iframeUi.realHtml 保持一致
        html = html.replace(/(<div.*?>)([\s\S]*)<\/div>/,"$1<template>$2</template></div>")
    }
    document.body.appendHTML(html);
    eval(javascript);
    {//追加样式
        if(css){
            document.getElementById("magicalCoderCss").innerHTML=css;
        }
    }
    {//追加magicalDragScene样式
        if(canvasStyle){
            //设置样式
            var style = [];
            for(var key in canvasStyle){
                style.push(key+":"+canvasStyle[key]);
            }
            document.body.setAttribute("style",style.join(";"));
        }
    }
}
{
    if(dependency.uiFrame=='vue'){
        var url = window.parent.baseUrl + "/base/formBase/" + getParameter("formId");
        axios({
            url: url,
            method: 'get',
            headers:{
                'Content-Type':'application/json;charset=utf-8',
                'Authorization': window.parent.token
            }
        }).then(function(response) {
            if(response.data.code === 200){
                var _res = response.data;
                _res.data.html = _res.data.html.replace(/&lt;/g,'<');
                _res.data.html = _res.data.html.replace(/&gt;/g,'>');
                console.log(_res)
                renderData(_res.data)
            }
        })
    }
}

